﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;

namespace FileManager
{
    enum FileType
    {
        File,
        Folder
    }
    struct FileInfo
    {
        public string Name;
        public FileType Type;
    }
    class Model
    {
        string m_leftPath, m_rightPath;
        List<FileInfo> m_leftList = new List<FileInfo>();
        List<FileInfo> m_rightList = new List<FileInfo>();
        string m_leftDrive, m_rightDrive;

        public Model()
        {
            setDrive("C:", true);
            setDrive("C:", false);
        }
        public List<string> getDriveList()
        {
            System.IO.Directory.GetLogicalDrives();
            string[] drives = Environment.GetLogicalDrives();
            List<string> driveList = new List<string>();
            foreach (string drive in drives)
            {
                driveList.Add(drive);
            }
            return driveList;
        }

        public void setDrive(string DriveName, bool Side)
        {
            if (Side)
            {
                m_rightDrive = DriveName;
                setPath(m_rightDrive, Side);
            }
            else
            {
                m_leftDrive = DriveName;
                setPath(m_leftDrive, Side);
            }
        }
        public List<FileInfo> getFileList(bool Side)
        {
            if (Side)
            {
                return m_rightList;
            }
            else
            {
                return m_leftList;
            }
        }
        public void itemDblClick(int item, bool Side)
        {
            if (Side)
            {
                if (m_rightList.ElementAt(item).Type == FileType.File)
                {
                    openFile(m_rightPath + "/" + m_rightList.ElementAt(item).Name);
                }
                else
                {
                    setPath(m_rightPath + "/" + m_rightList.ElementAt(item).Name, Side);
                }
            }
            else
            {
                if (m_leftList.ElementAt(item).Type == FileType.File)
                {
                    openFile(m_leftPath + "/" + m_leftList.ElementAt(item).Name);
                }
                else
                {
                    setPath( m_leftPath + "/" + m_leftList.ElementAt(item).Name, Side);
                    
                }
            }
        }
        public void setPath(string Path, bool Side)
        {
            if (Side)
            {
                m_rightPath = Path;
                m_rightList.Clear();
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Path); 
                foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
                {
                    FileInfo fi = new FileInfo();
                    fi.Name=f.Name;
                    fi.Type=FileType.File;

                    m_rightList.Add(fi);

                }


                foreach (System.IO.DirectoryInfo f in dir.GetDirectories())
                {
                    FileInfo fi = new FileInfo();
                    fi.Name = f.Name;
                    fi.Type = FileType.Folder;

                    m_rightList.Add(fi);
                }  
            }
            else
            {
                m_leftPath = Path;
                m_leftList.Clear();

                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Path);
                foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
                {
                    FileInfo fi = new FileInfo();
                    fi.Name = f.Name;
                    fi.Type = FileType.File;

                    m_leftList.Add(fi);

                }


                foreach (System.IO.DirectoryInfo f in dir.GetDirectories())
                {
                    FileInfo fi = new FileInfo();
                    fi.Name = f.Name;
                    fi.Type = FileType.Folder;

                    m_leftList.Add(fi);
                } 

            } 
        }
        public void copyFile(int Item, bool Side)
        {

        }
        public void deleteFile(int Item, bool Side)
        {
    
        }
        private void openFile(string FileName)
        {
            Process.Start("notepad.exe", FileName);
        }
    }
}
